

import { _decorator, Node, game, Game, EventKeyboard, tween, Tween, Vec3, systemEvent, SystemEvent, js, macro } from "cc";
import { Logic } from "../../logic/Logic";
import { Macro } from "../../Main/Macros";
import c3d from "../../tool/cc";
import AudioComponent from "./AudioComponent";


/**
 * 视图基类
 */
const { ccclass, property } = _decorator;

@ccclass
export default class UIView extends c3d.Component implements IFullScreenAdapt {
    static logicType: LogicClass<Logic> | null = null;
    protected _logic: Logic | null = null;
    protected get logic() { return this._logic; }
    
    /**由管理器统一设置，请勿操作 */
    setLogic(logic: Logic) {
        this._logic = logic;
        if (logic) {
            logic.onLoad(this);
        }
    }

    onFullScreenAdapt(): void {

    }
    /**获取prefab的地址 */
    static getPrefabUrl() { return c3d.error(`请求实现 static getPrefabUrl`), Macro.UNKNOWN; }

    /**ViewOption.args参数 */
    args?: any[] | null = null;
    /**统一定义一个显示内容节点*/
    content: Node | null = null;
    /**本组件的类名 */
    className: string = "unknow";
    /**指向当前View打开时的bundle */
    bundle: BUNDLE_TYPE = null!;


    audioHelper: AudioComponent = null!;
    onLoad() {
        this.audioHelper = <AudioComponent>(this.addComponent(AudioComponent));
        this.audioHelper.owner = this;
        super.onLoad();
        this.onEnterGameView()
    }

    onDestroy() {
        if (this.audioHelper) {
            //停止背景音乐
            //this.audioHelper.stopMusic();
            this.audioHelper.stopAllEffects();
        }
        if (this.logic) {
            GM.logic.destory(this.logic.bundle);
        }
        GM.entryMgr.onDestroyGameView(this.bundle, this);
        this.enableFrontAndBackgroundSwitch = false;
        super.onDestroy();
    }

    update(dt: number) {
        if (this.logic) {
            this.logic.update(dt);
        }
    }

    /**游戏重置 */
    protected reset() {
        if (this.logic) {
            this.logic.reset(this);
        }
    }

    enterBundle(bundle: BUNDLE_TYPE, isQuitGame: boolean = false) {
        GM.entryMgr.enterBundle(bundle, isQuitGame);
    }


    protected onEnterGameView() {
        GM.entryMgr.onEnterGameView(this.bundle, this);
    }

    /**关闭界面动画 */
    protected get closeAction(): ViewAction | null { return null; }
    /**显示界面动画 */
    protected get showAction(): ViewAction | null { return null; }
    /**隐藏界面动画 */
    protected get hideAction(): ViewAction | null { return null; }

     close() {
        if (this.closeAction) {
            this.closeAction(() => {
                GM.uiMgr.close(this.className);
            });
        } else {
            GM.uiMgr.close(this.className);
        }
    }

    /**args为open代入的参数 */
     show(args?: any[] | any) {
        //再如果界面已经存在于界面管理器中，此时传入新的参数，只从show里面过来,这里重新对_args重新赋值
        this.args = args;
        if (this.node) this.node.active = true;
        if (this.showAction) {
            this.showAction(() => { });
        }
        GM.entryMgr.onShowGameView(this.bundle, this);
    }

     hide() {
        if (this.hideAction) {
            this.hideAction(() => {
                if (this.node) this.node.removeFromParent();
            });
        } else {
            if (this.node) this.node.removeFromParent();
        }
    }

    /*****后台切换时间有关 */

    /**进入后台的时间 */
    private _enterBackgroundTime = 0;
    /** 是否开启前后台切换时监听切换时长*/
    private _enableFrontAndBackgroundSwitch = false;
    protected get enableFrontAndBackgroundSwitch() { return this._enableFrontAndBackgroundSwitch; }
    protected set enableFrontAndBackgroundSwitch(value) {
        this._enableFrontAndBackgroundSwitch = value;
        if (value) {
            game.on(Game.EVENT_SHOW, this._onEnterForgeGround, this);
            game.on(Game.EVENT_HIDE, this._onEnterBackground, this);
        } else {
            game.off(Game.EVENT_SHOW, this._onEnterForgeGround, this);
            game.off(Game.EVENT_HIDE, this._onEnterBackground, this);
        }
    }

    private _onEnterBackground() {
        this._enterBackgroundTime = Date.timeNow();
        this.onEnterBackground();
    }
    private _onEnterForgeGround() {
        let now = Date.timeNow();
        let inBackgroundTime = now - this._enterBackgroundTime;
        this.onEnterForgeground(inBackgroundTime);
    }

    /**
     *  进入前台 cc.game.EVENT_SHOW
     * @param inBackgroundTime 在后台运行的总时间，单位秒
     */
    protected onEnterForgeground(inBackgroundTime: number) { }
    /*进入后台  */
    protected onEnterBackground() { }
}